package main;

import java.io.IOException;
import java.util.ArrayList;

public class Sjavac {
	
	public static String line;
	public static ReadTextFile commands;
	
	public static ArrayList<Variable> members = new ArrayList<Variable>();
	public static ArrayList<Method> methods = new ArrayList<Method>();
	
	/**
	 * run over the whole file, 
	 * if the code are invalid by the Simple Java rules it's will throw compilationException
	 * @param args - The path of the program to compile
	 * @throws compilationException
	 * @throws IOException
	 */
	public static void main(String[] args) throws compilationException, IOException {
		
		String javacFile=args[0]+"103.sjava";
		commands = new ReadTextFile(javacFile);
	
		nextLine ();
		while (line!=null) {
			TextProcessor.cleanCode();
			
			// If the line is a variable declaration
			if (CheckType.isCommand(line)){
				addUniqVar (CorrectnessAndHandle.handleVariable(line,false), members);
				nextLine();
				continue;
			}
			
			// If the line is a beginning of a method
			if (CheckType.isBlock(line)){
				addUniqMethod (CorrectnessAndHandle.handleMethod(line), methods);
				continue;
			}
			
			throw new compilationException("Error 1: UnKnown command:"+ line);
		}
		
		commands.close ();
		
		for (int i=0; i<methods.size(); i++) {
			methods.get(i).checkCorrectness();
		}
	}
	
	public static String nextLine () throws IOException {
		line = commands.readLine();
		System.out.println(line);
		return line;
	}
	
	public static void addUniqVar (Variable data, ArrayList<Variable> array) throws compilationException {
		for (Variable m: array) {
			if (m.getName().equals(data.getName()))
				throw new compilationException("Error 18: data can't initial more than one");
		}
		array.add(data);
	}
	public static void addUniqMethod (Method data, ArrayList<Method> array) throws compilationException {
		for (Method m: array) {
			if (m.getName().equals(data.getName()))
				throw new compilationException("Error 18: data can't initial more than one");
		}
		array.add(data);
	}
}
